package com.ambow.common.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Slf4j
public class RequestLog extends HandlerInterceptorAdapter {
    /**
     * 前置检查
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            log.info("请求url=[{}],IP=[{}],请求方式=[{}],请求类路径={}",request.getRequestURI(),request.getRemoteAddr(),request.getMethod(), method.getDeclaringClass().getName() + "." + method.getName());
        }
        return true;
    }


}
